<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="项目编号" prop="projectNumber">
        <el-input
          v-model="queryParams.projectNumber"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否完结" prop="isProjectCompleted">
        <el-input
          v-model="queryParams.isProjectCompleted"
          placeholder="请输入是否完结"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否结清" prop="isAmountSettled">
        <el-input
          v-model="queryParams.isAmountSettled"
          placeholder="请输入是否结清"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="立项编号" prop="initiationId">
        <el-input
          v-model="queryParams.initiationId"
          placeholder="请输入立项编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应付类型" prop="isNumber">
        <el-input
          v-model="queryParams.isNumber"
          placeholder="请输入应付类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:cope:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cope:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cope:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:cope:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="copeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="应付编号" align="center" prop="id" />
      <el-table-column label="项目编号" align="center" prop="projectNumber" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="报价金额" align="center" prop="quotationAmount" />
      <el-table-column
        label="结算金额"
        align="center"
        prop="settlementAmount"
      />
      <el-table-column
        label="负责人姓名"
        align="center"
        prop="personInCharge"
      />
      <el-table-column
        label="开工日期"
        align="center"
        prop="startDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="竣工日期"
        align="center"
        prop="completionDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completionDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="是否完结"
        align="center"
        prop="isProjectCompleted"
      />
      <el-table-column label="是否结清" align="center" prop="isAmountSettled" />
      <el-table-column label="立项编号" align="center" prop="initiationId" />
      <el-table-column label="供应商名" align="center" prop="supplierName" />
      <el-table-column label="应付类型" align="center" prop="isNumber" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:cope:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cope:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商应付汇总对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目编号" prop="projectNumber">
          <el-input v-model="form.projectNumber" placeholder="请输入项目编号" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="报价金额" prop="quotationAmount">
  <el-input
    v-model="form.quotationAmount"
    placeholder="仅允许输入数字"
    @input="handleInputQuotedPrice"
    show-word-limit
  />
</el-form-item>

        <el-form-item label="结算金额" prop="settlementAmount">
          <el-input
            v-model="form.settlementAmount"
            placeholder="请输入结算金额"
          />
        </el-form-item>
        <el-form-item label="负责人姓名" prop="personInCharge">
          <el-input
            v-model="form.personInCharge"
            placeholder="请输入负责人姓名"
          />
        </el-form-item>
        <el-form-item label="开工日期" prop="startDate">
          <el-date-picker
            clearable
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开工日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="竣工日期" prop="completionDate">
          <el-date-picker
            clearable
            v-model="form.completionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择竣工日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否完结" prop="isProjectCompleted">
          <el-input
            v-model="form.isProjectCompleted"
            placeholder="请输入是否完结"
          />
        </el-form-item>
        <el-form-item label="是否结清" prop="isAmountSettled">
          <el-input
            v-model="form.isAmountSettled"
            placeholder="请输入是否结清"
          />
        </el-form-item>
        <el-form-item label="立项编号" prop="initiationId">
          <el-input v-model="form.initiationId" placeholder="请输入立项编号" />
        </el-form-item>
        <el-form-item label="供应商名" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商名" />
        </el-form-item>
        <el-form-item label="应付类型" prop="isNumber">
          <el-input v-model="form.isNumber" placeholder="请输入应付类型" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCope,
  getCope,
  delCope,
  addCope,
  updateCope,
} from "@/api/incomeExpenses/cope";
import { handleInputQuotedPrice } from '@/utils/myUtil';

export default {
  name: "Cope",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商应付汇总表格数据
      copeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectNumber: null,
        projectName: null,
        isProjectCompleted: null,
        isAmountSettled: null,
        initiationId: null,
        isNumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectNumber: [
          { required: true, message: "项目编号不能为空", trigger: "blur" },
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        quotationAmount: [
          { required: true, message: "报价金额不能为空", trigger: "blur" },
        ],
        settlementAmount: [
          { required: true, message: "结算金额不能为空", trigger: "blur" },
        ],
        personInCharge: [
          { required: true, message: "负责人姓名不能为空", trigger: "blur" },
        ],
        startDate: [
          { required: true, message: "开工日期不能为空", trigger: "blur" },
        ],
        completionDate: [
          { required: true, message: "竣工日期不能为空", trigger: "blur" },
        ],
        isProjectCompleted: [
          { required: true, message: "是否完结不能为空", trigger: "blur" },
        ],
        isAmountSettled: [
          { required: true, message: "是否结清不能为空", trigger: "blur" },
        ],
        initiationId: [
          { required: true, message: "立项编号不能为空", trigger: "blur" },
        ],
        supplierName: [
          { required: true, message: "供应商名不能为空", trigger: "blur" },
        ],
        isNumber: [
          { required: true, message: "应付类型不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供应商应付汇总列表 */
    getList() {
      this.loading = true;
      listCope(this.queryParams).then((response) => {
        this.copeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectNumber: null,
        projectName: null,
        quotationAmount: null,
        settlementAmount: null,
        personInCharge: null,
        startDate: null,
        completionDate: null,
        isProjectCompleted: null,
        remarks: null,
        isAmountSettled: null,
        initiationId: null,
        supplierName: null,
        isNumber: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商应付汇总";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCope(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商应付汇总";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCope(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCope(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除供应商应付汇总编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCope(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/cope/export",
        {
          ...this.queryParams,
        },
        `cope_${new Date().getTime()}.xlsx`
      );
    },
    handleInputQuotedPrice(value) {
      // 调用工具函数
      this.form.quotedPrice = handleInputQuotedPrice(value);
    },
  },
};
</script>
